/**
 * Created by Administrator on 2017/6/28.
 */

const userDao = require('../mysql/userDao.js');
const log = require('../util/log.js');
const create = require('./create.js');

module.exports = tourist;

function tourist(account, password, platform) {
    this._account = account;
    this._password = password;
    this._platform = platform;
    this._cb = null;
}


tourist.prototype.run = function (cb) {
    this._cb = cb;
    if (this._account) {
        this.login();
    } else {
        this.reg();
    }
};

tourist.prototype.login = function () {
    var _this = this;
    var userModel = new userDao();
    userModel.inAccount(_this._account, function (err, res) {
        if (res == null) {
            _this._cb(null, {'error': 102});
            return false;
        }
        if (!userModel.checkPass(_this._password)) {
            _this._cb(null, {'error': 103});
            return false;
        }
        create.reToken(userModel, _this._cb);
        return false;
    });
};

tourist.prototype.reg = function () {
    var _this = this;
    create.auto(function (err, res, data) {
        if (res == null) {
            _this._cb(null, {'error': 105, 'msg': '创建账号失败'});
            return false;
        }
        _this._cb(null, data);
        return false;
    });
};

